/**
Defines the custom str_echo function.
*/
#include "globals.h"

/**
Function to echo a string read from a socket.

@param sockfd supplies the socket descriptor.
*/
void
str_echo_ext(
    int sockfd
    )
{
	ssize_t		n;
	char		buf[MAXLINE];

again:
	while ( (n = ts_readline(sockfd, buf, MAXLINE)) > 0)
    {
		if (writen_ext(sockfd, buf, n) < 0)
        {
            //
            // Handle the EPIPE error. Terminate the client.
            //

            if (errno == EPIPE)
            {
                log_server_error(DIAG_ECHO_SRV_TERM_EPIPE, EPIPE);
                return;
            }
            else
            {
                err_sys("write error.");
            }
        }
    }

	if (n < 0 && errno == EINTR)
    {
		goto again;
    }
    else if (n < 0)
    {
		err_sys("str_echo: read error");
    }
    else if (n == 0)
    {
        //
        // Normal termination.
        //

        log_server(DIAG_ECHO_SRV_TERM);
    }
}

